package com.sxit.tdf.server.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxit.tdf.server.entity.Professional;
import com.sxit.tdf.server.entity.bo.ProfessionalInfoBo;
import com.sxit.tdf.server.entity.bo.SimpleProfessionalBo;
import com.sxit.tdf.server.entity.vo.SiftProfessionalInfoVo;
import com.sxit.tdf.server.exception.TdfException;
import com.sxit.tdf.server.mapper.ProfessionalMapper;
import com.sxit.tdf.server.service.IProfessionalService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * 主键 服务实现类
 *
 * @author 橙子高
 * @since 2022-02-08
 */
@Service
public class ProfessionalServiceImpl extends ServiceImpl<ProfessionalMapper, Professional>
    implements IProfessionalService {

  @Override
  public List<SimpleProfessionalBo> getSimpleProfessionalList() {
    return baseMapper.getSimpleProfessionalList();
  }

  @Override
  public IPage<ProfessionalInfoBo> getProfessionalList(SiftProfessionalInfoVo professionalInfoVo) {

    HashMap hashMap = JSONUtil.parse(professionalInfoVo).toBean(HashMap.class);
    System.out.println("-----" + hashMap);
    Page<ProfessionalInfoBo> page =
        new Page<ProfessionalInfoBo>(
            professionalInfoVo.getCurrent(), professionalInfoVo.getPageSize());

    return baseMapper.getProfessionalList(page, hashMap);
  }

  @Override
  public Boolean deleteByid(int id) {
    Boolean deleteToSource = baseMapper.isDeleteToSource(id);
    Boolean deteleToUser = baseMapper.isDeteleToUser(id);

    if (deleteToSource && deteleToUser) {
      int i = baseMapper.deleteById(id);
      System.out.println(i);
      return i == 1;
    } else {
      throw new TdfException("该专业下有用户或课程");
    }
  }

  @Override
  public List<SimpleProfessionalBo> getProfessionalByDeptId() {

    return baseMapper.getProfessionalByDeptId();
  }
}
